package org.example.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Category;
import org.example.entity.Note;
import org.example.service.CategoryService;
import org.example.utils.GetClassFields;
import org.example.utils.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;

/**
 * @author 谢娅
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/save")
    public Map<String,Object> save(@RequestBody Category category) {
        log.info("category:{}",category);
        if(StringUtils.isEmpty(category.getName())){
            return Result.fileMap("分类名称不能为空");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("name",category.getName());
        log.info("map:{}",map);
        Category byName = categoryService.getByName(category.getName());
        if(byName != null){
            return Result.fileMap("分类名称已存在");
        }
        boolean save = categoryService.save(category);
        if(!save){
            return Result.fileMap("分类保存失败");
        }
        Category returnCategory = categoryService.getByName(category.getName());
        return Result.successMap("分类保存成功",returnCategory);
    }

    @PostMapping("/getDimQuery")
    public Map<String,Object> getDimQuery(@RequestBody(required = false) Map<String,Object> dataMap){

        Map<String,Object> map = new HashMap<>();
        if (dataMap == null) {
            return Result.fileMap("参数为空，模糊查询失败");
        }

        Set<String> keys = dataMap.keySet();
        List<String> domainFields = GetClassFields.getDomainFields(new Note());
        domainFields.remove(domainFields.size()-1);
        System.out.println("domainFields"+domainFields);

        List<String> snakeCaseFields = new ArrayList<>();
        for(String field:domainFields){
            String s = GetClassFields.convertCamelCaseToSnakeCase(field);
            snakeCaseFields.add(s);
        }
        System.out.println("snakeCaseFields"+snakeCaseFields);

        for(String key:keys){
            if(!snakeCaseFields.contains(key)){
                if(!key.equals("startTime") && !key.equals("timeMax")){
                    dataMap.remove(key);
                }
            }
        }

        List<Category> dimQuery = categoryService.getDimQuery(dataMap);
        map.put("dimQuery",dimQuery);
        return Result.successMap(map);
    }

    @DeleteMapping("/remove/{id}")
    public Map<String,Object> remove(@PathVariable Serializable id) {
        Category category = categoryService.getById(id);
        if (category == null){
            return Result.fileMap("分类不存在");
        }
        return Result.successMap("分类删除成功");
    }

    @PutMapping("/update")
    public Map<String,Object> update(@RequestBody Category category) {
        if(category.getCategoryId() == null){
            return Result.fileMap("分类ID不能为空");
        }
        if(StringUtils.isEmpty(category.getName())){
            return Result.fileMap("分类名称不能为空");
        }
        boolean b = categoryService.updateById(category);
        if(!b){
            return Result.successMap("分类更新失败");
        }
        return Result.successMap("分类更新成功");
    }

    @GetMapping("/list")
    public Map<String,Object> list() {
        Map<String,Object> map = new HashMap<>();
        map.put("categoryList",new PageInfo<>(categoryService.list()));
        return Result.successMap(map);
    }

    @GetMapping("/getInfo/{id}")
    public Map<String,Object> getInfo(@PathVariable Serializable id) {
        return Result.successMap(categoryService.getById(id));
    }
}